import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-10-19
 * Time: 8:07
 */
public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root = null;

    /**
     * 查找val值是否在搜索树中，
     * @param val
     * @return 在搜索树中，返回目标节点，不在返回空
     */
    public TreeNode search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (val > cur.val) {
                cur = cur.right;
            } else if (val < cur.val) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 在当前二叉搜索树中插入元素
     * @param val
     * @return 插入成功返回true 失败返回false
     */
    public boolean insert(int val) {
        // 1. 当前树中没有节点 new一个新节点为根节点
        if (root == null) {
            root = new TreeNode(val);
            return true;
        }

        // 2. 定义cur走向叶子节点的下一个节点，定义一个parent指向cur的上一个节点
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else {
                // 插入的元素在当前的搜索树中已经存在，返回false
                return false;
            }
        }
        // 3. 这时，cur==null，parent指向树的叶子节点，将元素插入parent的孩子节点
        TreeNode node = new TreeNode(val);
        if (val > parent.val) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        // 先让cur找到目标节点
        while (cur != null) {
            if (cur.val == key) {
                removeNode(cur, parent);
                return;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    /**
     * 1. cur.left == null
     *      1. cur 是 root，则 root = cur.right
     *      2. cur 不是 root，cur 是 parent.left，则 parent.left = cur.right
     *      3. cur 不是 root，cur 是 parent.right，则 parent.right = cur.right
     * 2. cur.right == null
     *      1. cur 是 root，则 root = cur.left
     *      2. cur 不是 root，cur 是 parent.left，则 parent.left = cur.left
     *      3. cur 不是 root，cur 是 parent.right，则 parent.right = cur.left
     * @param cur
     * @param parent
     */
    private void removeNode(TreeNode cur, TreeNode parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            // 使用替罪羊法
            TreeNode target = cur.right;
            TreeNode targetParent = cur;

            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            // target走到cur的右树的最左边
            cur.val = target.val;
            // 删除target节点
            if (target == targetParent.left) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }
}
